package com.cskaoyan.controller;

import com.cskaoyan.bean.bo.CartAddBO;
import com.cskaoyan.bean.bo.WxCartCheckedBO;
import com.cskaoyan.bean.bo.WxCartDeleteBO;
import com.cskaoyan.bean.bo.WxCartUpdateBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.vo.wx_cart.WxCartCheckoutVO;
import com.cskaoyan.bean.vo.wx_cart.WxCartIndexVO;
import com.cskaoyan.service.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @Author: Ryan
 * @CreateTime: 2022-09-08  17:36
 */

@RestController
@RequestMapping("/wx/cart/*")
public class WxCartController {
    @Autowired
    WxCartService wxCartService;

    @RequestMapping("index")
    public BaseRespVo queryCartIndex() {
        WxCartIndexVO data = wxCartService.queryCartIndex();
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(data);
        return baseRespVo;
    }
    //http://182.92.235.201:8083/wx/cart/checkout?cartId=0&addressId=0&couponId=0&userCouponId=0&grouponRulesId=0
    @RequestMapping("checkout")
    public BaseRespVo queryCartCheckout(Integer cartId,Integer addressId,Integer couponId,Integer userCouponId,Integer grouponRulesId) {
        Session session = SecurityUtils.getSubject().getSession();
        MarketUser user = (MarketUser) session.getAttribute("info");

        WxCartCheckoutVO data = wxCartService.queryCartCheckout(cartId,addressId,couponId,userCouponId,grouponRulesId,user);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(data);
        return baseRespVo;
    }

    @RequestMapping("checked")
    public BaseRespVo cartChecked(@RequestBody WxCartCheckedBO wxCartCheckedBO) {
        WxCartIndexVO data = wxCartService.cartChecked(wxCartCheckedBO);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(data);
        return baseRespVo;
    }

    @RequestMapping("update")
    public BaseRespVo cartChecked(@RequestBody WxCartUpdateBO wxCartUpdateBO) {
        wxCartService.cartUpdate(wxCartUpdateBO);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");

        return baseRespVo;
    }
    @RequestMapping("delete")
    public BaseRespVo cartChecked(@RequestBody WxCartDeleteBO wxCartDeleteBO) {
        WxCartIndexVO wxCartIndexVO = wxCartService.cartdelete(wxCartDeleteBO);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(wxCartIndexVO);
        return baseRespVo;
    }
    @RequestMapping("goodscount")
    public BaseRespVo cartGoodscount() {
        Integer count = wxCartService.cartSelectGoodscount();
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(count);
        return baseRespVo;
    }
    @RequestMapping("add")
    public BaseRespVo cartAdd(@RequestBody CartAddBO cartAddBO) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getSession().getAttribute("info");

        Integer count = wxCartService.cartAdd(cartAddBO,user);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(count);
        return baseRespVo;
    }
    @RequestMapping("fastadd")
    public BaseRespVo cartFastadd(@RequestBody CartAddBO cartAddBO) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getSession().getAttribute("info");

        Integer cartId = wxCartService.cartFastadd(cartAddBO,user);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(cartId);
        return baseRespVo;
    }
}

